package com.cms.controllers.admin;

import com.alibaba.fastjson.JSON;
import com.cms.common.aop.permission.PermissionCheck;
import com.cms.common.model.entity.AdminUser;
import com.cms.common.model.entity.Permission;
import com.cms.common.model.entity.Roles;
import com.cms.common.model.services.AdminUserService;
import com.cms.common.model.services.PermissionService;
import com.cms.common.model.services.RolesService;
import com.cms.common.model.vo.PermissionVo;
import com.cms.common.model.vo.RequestData;
import com.cms.common.model.vo.ResponseResult;
import com.cms.controllers.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/roles")
public class RolesController extends BaseController {

    @Autowired
    RolesService rolesService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    AdminUserService adminUserService;

    @GetMapping("")
    @PermissionCheck(name = "角色列表", typeName = "角色管理")
    public String index() {
        return "admin/roles/index";
    }

    @GetMapping("/list")
    @ResponseBody
    @PermissionCheck(name = "角色列表数据", typeName = "角色管理")
    public ResponseResult roles(@ModelAttribute RequestData requestData) {
        Page<Roles> roles = rolesService.getRolesByPage(requestData);
        return ResponseResult.success(roles);
    }

    @GetMapping("/create")
    @PermissionCheck(name = "创建角色界面", typeName = "角色管理")
    public String create(Model viewModel) {
        //生成分组数据
        viewModel.addAttribute("roles", new Roles());
        List<PermissionVo> systemPermissionVos = permissionService.getAllPermissionVos(false);
        viewModel.addAttribute("systemPermissions", systemPermissionVos);
        List<PermissionVo> pluginsPermissionVos = permissionService.getAllPermissionVos(true);
        viewModel.addAttribute("pluginPermissions", pluginsPermissionVos);
        return "admin/roles/form";
    }

    @PostMapping("/store")
    @PermissionCheck(name = "创建角色", typeName = "角色管理")
    @ResponseBody
    public ResponseResult store(@RequestParam("role_name") String role_name, @RequestParam("permission_ids") String permission_ids) {

        List<Long> pIds = JSON.parseArray(permission_ids, Long.class);
        List<Permission> permissionList = permissionService.getPermissionsByIds(pIds);
        Roles roles = new Roles();
        roles.setName(role_name);
        roles.setPermissions(permissionList);
        roles = rolesService.addRoles(roles);
        return ResponseResult.success(roles);
    }

    @GetMapping("/edit/{id}")
    @PermissionCheck(name = "修改角色界面", typeName = "角色管理")
    public String edit(@PathVariable Long id, Model viewModel) {
        Roles roles = rolesService.getRolesId(id);
        viewModel.addAttribute("roles", roles);
        //生成分组数据
        List<PermissionVo> systemPermissionVos = permissionService.getAllPermissionVos(false, roles.getPermissions());
        viewModel.addAttribute("systemPermissions", systemPermissionVos);
        List<PermissionVo> pluginsPermissionVos = permissionService.getAllPermissionVos(true, roles.getPermissions());
        viewModel.addAttribute("pluginPermissions", pluginsPermissionVos);

        return "admin/roles/form";
    }

    @PutMapping("/update/{id}")
    @PermissionCheck(name = "修改角色", typeName = "角色管理")
    @ResponseBody
    public ResponseResult update(@PathVariable Long id, @RequestParam String role_name, @RequestParam("permission_ids") String permission_ids) {
        Roles roles = rolesService.getRolesId(id);
        List<Long> pIds = JSON.parseArray(permission_ids, Long.class);
        List<Permission> permissionList = permissionService.getPermissionsByIds(pIds);
        roles.setName(role_name);
        roles.setPermissions(permissionList);
        roles = rolesService.addRoles(roles);
        return ResponseResult.success(roles);
    }

    @DeleteMapping("/destroy/{id}")
    @PermissionCheck(name = "删除角色", typeName = "角色管理")
    @ResponseBody
    public ResponseResult destroy(@PathVariable Long id) {
        Roles roles = rolesService.getRolesId(id);
        List<AdminUser> adminUsers = adminUserService.findByRoles(roles);
        if (!adminUsers.isEmpty()) {
            return ResponseResult.fail("该角色下面存在后台用户，不能删除");
        }
        rolesService.deleteRoles(roles);
        return ResponseResult.success(roles);
    }
}
